package com.example.chatchatbackend.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.chatchatbackend.entity.dto.Shopping;

import com.example.chatchatbackend.entity.vo.response.ShoppingVO;
import com.example.chatchatbackend.mapper.ShoppingMapper;

import com.example.chatchatbackend.service.ShoppingService;
import com.example.chatchatbackend.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Service
public class ShoppingServiceImpl extends ServiceImpl<ShoppingMapper, Shopping> implements ShoppingService {

    private static final LocalDateTime currentTime = TimeUtil.Time_Now.getCurrentTime();

    @Autowired
    private ShoppingMapper shoppingMapper;



    @Override
    public List<Shopping> getAll() {
        return shoppingMapper.getAll();
    }

    @Override
    public ShoppingVO insertShopping(ShoppingVO shoppingVO) {
        Shopping shopping = new Shopping(null,shoppingVO.getProductId(),shoppingVO.getAddress(),shoppingVO.getStatus(),shoppingVO.getTrackingNumber(),this.currentTime,this.currentTime,shoppingVO.getCustomerId());
        if(this.save(shopping))
            return shoppingVO;
        return null;
    }

    @Override
    public List<ShoppingVO> insertShoppingList(List<ShoppingVO> shoppingVOList) {

        List<Shopping> shoppingList = new ArrayList<>();

        for (ShoppingVO shoppingVO : shoppingVOList) {
            shoppingList.add(new Shopping(null,shoppingVO.getProductId(),shoppingVO.getAddress(),shoppingVO.getStatus(),shoppingVO.getTrackingNumber(),this.currentTime,this.currentTime,shoppingVO.getCustomerId()));
        }
        shoppingMapper.insertList(shoppingList);
        return shoppingVOList;
    }


    @Override
    public IPage<Shopping> pageThree(Integer pageNum, Integer pageSize) {
        Page<Shopping> page = new Page<>(pageNum, pageSize);
        IPage<Shopping> shoppingIPage = shoppingMapper.selectAll(page, null);
        return shoppingIPage;
    }

    @Override
    public ShoppingVO updateShopping(ShoppingVO shoppingVO) {
        Shopping shopping = new Shopping();
        BeanUtils.copyProperties(shoppingVO, shopping);
        shopping.setUpdateTime(this.currentTime);
        shoppingMapper.updateShopping(shopping);
        return shoppingVO;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        shoppingMapper.deleteBatchIds(ids);
    }

    @Override
    public List<ShoppingVO> findByIds(List<Integer> ids) {
        List<Shopping> shoppingList = shoppingMapper.selectBatchIds(ids);
        List<ShoppingVO> shoppingVOList = new ArrayList<>();

        for (Shopping shopping : shoppingList) {
            ShoppingVO shoppingVO = new ShoppingVO();
            BeanUtils.copyProperties(shopping, shoppingVO);
            shoppingVOList.add(shoppingVO);  // 添加到结果列表
        }

        return shoppingVOList;
    }

}
